package com.lykj.llw.box.ui.activity

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.adapter.IntegralRecordAdapter
import com.lykj.llw.box.data.bean.response.IntegralRecordListResponse
import com.lykj.llw.box.databinding.ActIntegralExchangeRecordBinding
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 积分兑换记录
 * ================================================
 *
 * @author gosha
 * @date 2023/10/18-13:45
 */
class IntegralExchangeRecordActivity : BaseActivity() {
    lateinit var binding: ActIntegralExchangeRecordBinding

    private val mAdapter = IntegralRecordAdapter()
    private val mData = ArrayList<IntegralRecordListResponse.DataDTO.RowsDTO>()

    private var currentPage = 1
    private val pageSize = 10
    private var totalPage = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActIntegralExchangeRecordBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    private fun initRecyclerView() {
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rcvIntegralRecord.layoutManager = linearLayoutManager
        binding.rcvIntegralRecord.setHasFixedSize(true)
        mAdapter.data = mData
        val emptyView = View.inflate(this, R.layout.empty_playing, null)
        emptyView.findViewById<TextView>(R.id.tv_desc).text = "暂无积分兑换记录"
        mAdapter.setEmptyView(emptyView)
        binding.rcvIntegralRecord.adapter = mAdapter
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "积分兑换记录"
        binding.titleBar.tvRightOption.visibility = View.GONE

        initRecyclerView()
    }

    //获取钱包记录
    private fun obtainIntegralRecord() {
        clearBodyParam()
        initBodyParam()
        bodyParams["pageNo"] = currentPage
        bodyParams["pageSize"] = pageSize
        RetrofitManager.instance.obtainIntegralRecord(
            bodyParams,
            object : CustomResponseBodyObserver<ResponseBody>() {
                override fun onSubscribe(d: Disposable) {
                    addDisposables(this, d)
                }

                override fun specificHandle(responseBody: String) {
                    val bean =
                        GsonUtils.fromJson(responseBody, IntegralRecordListResponse::class.java)
                    totalPage = bean.data.totalPage
                    if (!bean.data.rows.isNullOrEmpty()) {
                        mData.addAll(bean.data.rows)
                    }

                    mAdapter.notifyDataSetChanged()
                }

                override fun handleException(ret: String, msg: String?) {
                    msg?.let {
                        ToastUtils.showShort(this@IntegralExchangeRecordActivity, it)
                    }
                }
            })
    }

    override fun initData() {
        obtainIntegralRecord()
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.srfRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                currentPage = 1
                mData.clear()
                mAdapter.notifyDataSetChanged()

                obtainIntegralRecord()

                refreshLayout.finishRefresh(500, true, false)
                refreshLayout.resetNoMoreData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (currentPage == totalPage) {
                    refreshLayout.finishLoadMoreWithNoMoreData()
                    ToastUtils.showShort(this@IntegralExchangeRecordActivity, "没有更多数据了")
                    return
                }
                refreshLayout.finishLoadMore(500, true, false)
                currentPage++
                obtainIntegralRecord()
            }
        })
    }

    override fun onDestroy() {
        removeDisposables(this)
        super.onDestroy()
    }
}